package com.phoxell;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import java.io.IOException;
import java.io.Reader;

public class DataImg{
	private static final int FDAY=3600*24;
	private static final int LMIT=60;
	private static final int TENM=600;

	private Entity ent;
	private byte[] img;		// data
	private int ofs;
	private String pml;		// serial number
	private long tmc;		// create time
	private long tme;		// time

	public DataImg(String permalink){
		ent=new Entity("i601",permalink);
		if(permalink!=null){
			pml=permalink;
			try{
				Key k=KeyFactory.createKey("i601",pml);
				DatastoreService d=DatastoreServiceFactory.getDatastoreService();
				Entity e=d.get(k);
				img=((Blob)e.getProperty("img")).getBytes();
				tmc=(Long)e.getProperty("c");
				tme=(Long)e.getProperty("t");
				return;
			}catch(Exception e){
				//e.printStackTrace();
			}
		}
		tmc=System.currentTimeMillis();
	}

	public boolean check(int offset){
		if(offset<0)
			return false;
		if(offset>=LMIT)
			return false;
		int d=offset<<2;
		return img[d+4]!=0 && img[d+5]!=0;
	}
	public long getTime(){
		if(ofs==0)
			return tme;
		int d=(ofs<<2)+4;
		int i=(img[d++]&0xff)+((img[d]<<8)&0xff00)+4;
		int v=0;
		for(int f=0;f<32;f+=8)
			v|=(img[i++]&0xff)<<f;
		return (long)(v-v%FDAY)*1000;
	}
	public int outBits(PageFrame page){
		int d=ofs<<2;
		int k=(img[d]&0xff)+((img[d+1]<<8)&0xff00);
		int i=(img[d+4]&0xff)+((img[d+5]<<8)&0xff00);
		int f=0,v=0;
		int a=0,b=0,r=0,s=0,t=0;
		boolean z=false;
		while(i<k){
			v|=(img[i++]&0xff)<<f;
			if(f==24){
				if(z){
					t=v;
					if(b==0)
						b=t-t%FDAY;
					z=false;
					a+=s;
					outItem(page,b,t,s);
				}else{
					r=v&0xffff;
					s=v>>16;
					if(r==0)
						z=true;
					else{
						t+=r;
						a+=s;
						outItem(page,b,t,s);
					}
				}
				v=0;
			}
			f=(f+8)&0x1f;
		}
		return a;
	}
	public int outBits2(PageFrame page){
		page.print("<table><tr><td>0000&nbsp;&nbsp;<td>");
		for(int i=0,n=img.length;i<n;){
			page.print(String.format("%02X",img[i++]));
			if(i%4==0){
				if(i%32==0){
					page.print("<tr><td>");
					page.print(String.format("%04X",i));
				}
				page.print("<td>");
			}
		}
		page.print("</table>");
		return 0;
	}
	public int outGraf(PageFrame page){
		int d=ofs<<2;
		page.print("<div class=canv><div class=grd1></div>"+
				"<div class=grd2></div><div class=grdh></div>");
		int k=(img[d]&0xff)+((img[d+1]<<8)&0xff00);
		int i=(img[d+4]&0xff)+((img[d+5]<<8)&0xff00);
		int f=0,v=0;
		int a=0,b=0,c=0,t=0,r=0,s=0,x=1;
		boolean z=false;
		while(i<k){
			v|=(img[i++]&0xff)<<f;
			if(f==24){
				if(z){
					if(b==0){
						b=v-v%FDAY;
						c=b+TENM;
					}
					t=v;
					z=false;
				}else{
					r=v&0xffff;
					s+=v>>16;
					if(r==0)
						z=true;
					else t+=r;
				}
				if(t>=c){
					while(c<=t){
						if(s!=0){
							a+=s;
							page.print("<img src=/media/blank.gif style=left:");
							page.print(x);
							page.print("px;height:");
							page.print((s=s/15)==0?1:s);
							page.print("%>");
						}
						c+=TENM;
						x+=3;
						s=0;
					}
				}
				v=0;
			}
			f=(f+8)&0x1f;
		}
		if(s!=0){
			a+=s;
			page.print("<img src=/media/blank.gif style=left:");
			page.print(x);
			page.print("px;height:");
			page.print((s=s/15)==0?1:s);
			page.print("%>");
		}
		page.print("<div class=gs02>1k</div><div class=gs01>500</div>"+
				"</div><div class=gfoo><div class=gt00>0</div>"+
				"<div class=gt06>6</div><div class=gt12>12</div>"+
				"<div class=gt18>18</div><div class=gt24>24</div></div>");
		return a;
	}
	private void outItem(PageFrame page,int base,int tick,int steps){
		int d=tick-base;
		page.print(String.format("%02d:%02d ",d/3600,d%3600/60));
		page.printNz(steps);
		page.print("<br>");
	}
	public void query(String permalink){
	}
	public void setImage(int value,Reader reader)throws IOException{
		System.out.println(String.format("%08x",value));
		byte[] s=new byte[0x10000];
		int n,x;
		for(n=0;(x=reader.read())!=-1 && n<0x10000;)
			s[n++]=(byte)x;

		int m=n+228;
		img=new byte[m];
		short[] b=new short[m/8];
		boolean f=false;
		int dys=0,shf=0,v=0;
		short k=0;
		img[0]=(byte)m;
		img[1]=(byte)(m>>8);
		for(int o=0,p=228;o<n;){
			x=s[o++]&0xff;
			img[p++]=(byte)x;
			v|=x<<shf;
			//System.out.print(String.format("%02x ",x));
			if(shf==24){
				if(f){
					int c=v/86400;
					if(dys!=c){
						b[k++]=(short)(p-8);
						dys=c;
						//System.out.println(String.format(" * %08x",v));
					}//else System.out.println(String.format(" - %08x",v));
					f=false;
				}else if((v&0xffff)==0){
					//System.out.println(String.format("   %08x",v));
					f=true;
				}//else System.out.println(String.format(" - %08x",v));
				v=0;
			}
			shf=(shf+8)&0x1f;
		}
		for(m=4,n=k>56?k-56:0;k>n;){
			x=b[--k];
			img[m++]=(byte)x;
			img[m]=(byte)(x>>8);
			m+=3;
		}
	}
	public void setOffset(int value){
		ofs=value;
	}
	public void setTime(long value){
		tme=value;
	}
	public String toString(){
		return pml;
	}
	public int update()throws Exception{
		ent.setProperty("img",new Blob(img));
		ent.setProperty("c",tmc);
		ent.setProperty("p",pml);
		ent.setProperty("t",System.currentTimeMillis());
		DatastoreService das=DatastoreServiceFactory.getDatastoreService();
		das.put(ent);
		return 0;
	}
}